Imports System.IO
Imports System.Data
Imports System.Data.SqlClient

Public Class frmCONFIGURATION
    Dim index As Integer = 1

    Private Sub LoadConfiguration()
        Dim IniFile As String
        IniFile = Application.StartupPath + "\TOTALPOS.ini"
        If File.Exists(IniFile) Then

            Dim objIniFile As New INIClass(IniFile)
            Dim SQLServer As String = objIniFile.GetString("SQL", "Server", "local")
            Dim SQLPass As String = objIniFile.GetString("SQL", "SAPWD", "sa")
            Dim SQLDB As String = objIniFile.GetString("SQL", "SQLDB", "VINAPOS")

            txtDBServer.Text = SQLServer
            txtSAPwd.Text = o3DES.DES.Decrypt(SQLPass)
            txtDBName.Text = SQLDB
        Else
            MessageBox.Show("Read configuration file error!")
            Return
        End If
        If PublicVariable.IsConnect Then
            GridConfigure.DataSource = BLL.ObjectGetAll("CONFIGURATION_LOADALL")
            GridTables.DataSource = oConfigure.LoadAllDBTable.DefaultView
        End If

    End Sub

    Private Sub frmCONFIGURATION_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Top = 0
        Me.Left = 0

        LoadConfiguration()

    End Sub

    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        LoadConfiguration()
    End Sub

    Private Sub GridConfigure_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridConfigure.SelectionChanged
        If GridConfigure.CurrentRow.RowType = Janus.Windows.GridEX.RowType.Record Then
            txtCategory.Text = GridConfigure.GetValue("Category").ToString
            txtSubCategory.Text = GridConfigure.GetValue("SubCategory").ToString
            txtName.Text = GridConfigure.GetValue("Name").ToString
            txtValue.Text = GridConfigure.GetValue("Value").ToString
            txtDecription.Text = GridConfigure.GetValue("Description").ToString
        End If

    End Sub

    Private Sub GridTables_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridTables.SelectionChanged
        TableData.DataSource = oConfigure.LoadTableData(GridTables.GetValue("Tables").ToString).DefaultView
        TableData.RetrieveStructure()
    End Sub

    Private Sub btnSaveSQL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveSQL.Click
        Dim IniFile As String
        IniFile = Application.StartupPath + "\TOTALPOS.ini"
        If File.Exists(IniFile) Then
            Dim objIniFile As New INIClass(IniFile)
            objIniFile.WriteString("SQL", "SAPWD", o3DES.DES.Encrypt(txtSAPwd.Text))
            objIniFile.WriteString("SQL", "Server", txtDBServer.Text)
            objIniFile.WriteString("SQL", "SQLDB", txtDBName.Text)
            PublicVariable.ConnectionString = "data source=" + txtDBServer.Text + ";initial catalog=" + txtDBName.Text + ";persist security info=True;packet size=4096;user id=sa;password=" + txtSAPwd.Text
            MessageBox.Show("SAVE COMPLETED!")
        End If

    End Sub

    Private Sub txtTestConnection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTestConnection.Click
        Dim str As String = PublicVariable.ConnectionString
        PublicVariable.ConnectionString = "data source=" + txtDBServer.Text + ";initial catalog=" + txtDBName.Text + ";persist security info=True;packet size=4096;user id=sa;password=" + txtSAPwd.Text
        If PublicFunction.OpenSQLConnection() = False Then
            MessageBox.Show("Can't connect to database server!")
        Else
            MessageBox.Show("Connection sucessfull!")
            PublicVariable.Connection.Close()
        End If
        PublicVariable.ConnectionString = str
    End Sub

    Private Sub btnBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackup.Click
        SaveFileDialog1.FileName = CStr(Now.Year) + CStr(Now.Month) + CStr(Now.Day) + "_TOTALPOS.bak"
        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            oConfigure.BackupDB(SaveFileDialog1.FileName, txtDBName.Text)
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If GridConfigure.CurrentRow.RowType = Janus.Windows.GridEX.RowType.Record Then
            oConfigure.UpdateConfiguration(GridConfigure.CurrentRow.Cells("NAME").Value, txtValue.Text)

        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click
        GridQuery.DataSource = oConfigure.RunQuery(txtQuery.Text)
        GridQuery.RetrieveStructure()

    End Sub

    Private Sub btnCreateDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateDB.Click
        CreateDB_Master()
    End Sub
    Private Sub CreateDB_Master()
        Dim SqlConnection_Master = New SqlConnection
        Dim str As String
        str = "data source=" + txtDBServer.Text + ";initial catalog=master;persist security info=True;packet size=4096;user id=sa;password=" + txtSAPwd.Text
        SqlConnection_Master.ConnectionString = str
        If PublicVariable.Connection.State = ConnectionState.Open Then
            SqlConnection_Master.Close()
        End If
        Try
            SqlConnection_Master.Open()
            Dim strQuery As String
            strQuery = "Create database " + txtDBName.Text
            Dim MyCommand As SqlCommand = New SqlCommand(strQuery, SqlConnection_Master)
            MyCommand.CommandType = CommandType.Text
            MyCommand.ExecuteNonQuery()
            SqlConnection_Master.Close()
            MessageBox.Show("CREATE NEW DATABASE COMPLETED!")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub


    Private Sub btnRestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestore.Click
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            oConfigure.RestoreDB(OpenFileDialog1.FileName, txtDBName.Text)
        End If

    End Sub

    Private Sub txtDBServer_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDBServer.Enter
        index = 1
    End Sub

    Private Sub txtSAPwd_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSAPwd.Enter
        index = 2
    End Sub

    Private Sub txtDBName_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDBName.Enter
        index = 3
    End Sub

    Private Sub Keyboardcontrol1_UserKeyPressed(ByVal sender As System.Object, ByVal e As VINAPOS.KeyboardClassLibrarySmall.KeyboardEventArgs) Handles Keyboardcontrol1.UserKeyPressed
        Select Case index
            Case 1
                txtDBServer.Focus()
            Case 2
                txtSAPwd.Focus()
            Case 3
                txtDBName.Focus()
        End Select
        SendKeys.Send(e.KeyboardKeyPressed)
    End Sub

    Private Sub btnReg_Click(sender As System.Object, e As System.EventArgs) Handles btnReg.Click
        Dim a As New ServiceController(txtServiceName.Text)
        a.Description = txtServiceName.Text
        a.DisplayName = txtServiceName.Text
        a.ServiceName = txtServiceName.Text
        a.StartupType = ServiceController.ServiceStartupType.Automatic

        Dim sReturn As String
        sReturn = a.Register(Application.ExecutablePath + " -service")
        MessageBox.Show(Application.ExecutablePath + " -service")
        If sReturn = "" Then
            MessageBox.Show("Register Sucessfull!")
        Else
            MessageBox.Show("Error: " + sReturn)
        End If
        If a.Status = "" Then
            btnReg.Enabled = True
            btnUnReg.Enabled = False
        Else
            btnReg.Enabled = False
            btnUnReg.Enabled = True
        End If
    End Sub

    Private Sub btnUnReg_Click(sender As System.Object, e As System.EventArgs) Handles btnUnReg.Click
        Dim a As New ServiceController(txtServiceName.Text)
        Dim sReturn As String
        sReturn = a.Unregister()
        If sReturn = "" Then
            MessageBox.Show("UnRegister Sucessfull!")
        Else
            MessageBox.Show("Error: " + sReturn)
        End If
        If a.Status = "" Then
            btnReg.Enabled = True
            btnUnReg.Enabled = False
        Else
            btnReg.Enabled = False
            btnUnReg.Enabled = True
        End If
    End Sub
End Class